package com.cuitrip.business.home.favorite.proxy;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;

import com.cuitrip.apiservice.TripBusiness;
import com.cuitrip.business.home.favorite.FavoriteListFragment;
import com.cuitrip.business.home.favorite.TravelTrip;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.rtenvironment.AppCoreRuntime;

/**
 * 收藏相关逻辑
 * Created by ak on 15/9/23.
 */
public class FavProxy {

    private static FavProxy sInstance;

    public static FavProxy getInstance() {
        if (null == sInstance)
            sInstance = new FavProxy();
        return sInstance;
    }

    public void favTrip(final String sid, String serviceName, String serviceAddress, String servicePic
            , String insiderId, String insiderNick, String insiderPic, String insiderCarrer, final Callback callback) {
        TripBusiness.likeService(new ApiProxy(new IProxyCallback() {
                    @Override
                    public boolean onProxyResult(ProxyResult<?> result) {
                        CtApiResponse response = (CtApiResponse) result.getData();
                        if (response != null && response.code == CtResponseCode.RESULT_OK) {
                            if (callback != null) {
                                Intent intent = new Intent(FavoriteListFragment.FAV_CHANGED_ACTION);
                                intent.putExtra(TravelTrip.KEY_TRAVEL_TRIP_ID, sid);
                                intent.putExtra(FavoriteListFragment.FAV_CHANGED_VALUE_KEY, true);
                                AppCoreRuntime.context.sendBroadcast(intent);
                                callback.onSuccess();
                            }
                        } else {
                            String msg;
                            if (response != null && !TextUtils.isEmpty(response.msg)) {
                                msg = response.msg;
                            } else {
                                msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                            }
                            if (callback != null) {
                                callback.onFail(msg);
                            }
                        }

                        return false;
                    }
                }), sid, serviceName, serviceAddress, servicePic,
                insiderId, insiderNick, insiderPic, insiderCarrer);
    }

    public void unFavTrip(final String tripId, final Callback callback){
        TripBusiness.unlikeService(new ApiProxy(new IProxyCallback() {
            @Override
            public boolean onProxyResult(ProxyResult<?> result) {
                CtApiResponse response= (CtApiResponse) result.getData();
                if(response !=null && response.code==CtResponseCode.RESULT_OK){
                    if (callback != null) {
                        callback.onSuccess();
                    }
                    Intent intent = new Intent(FavoriteListFragment.FAV_CHANGED_ACTION);
                    intent.putExtra(TravelTrip.KEY_TRAVEL_TRIP_ID, tripId);
                    intent.putExtra(FavoriteListFragment.FAV_CHANGED_VALUE_KEY, false);
                    AppCoreRuntime.context.sendBroadcast(intent);
                }else{
                    String msg;
                    if (response != null && !TextUtils.isEmpty(response.msg)) {
                        msg = response.msg;
                    } else {
                        msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                    }
                    if (callback != null) {
                        callback.onFail(msg);
                    }

                }
                return false;
            }
        }),tripId);
    }

    public void updateFavIcon(ImageView imageView, boolean isFav){
        if (isFav){
            imageView.setImageLevel(1);
        }else{
            imageView.setImageLevel(0);
        }
    }

    public interface Callback{

        public void onSuccess();

        public void onFail(String message);

    }


}
